package com.eogo.item.web;

import com.eogo.item.pojo.Category;
import com.eogo.item.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类
 */
@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 根据父节点ID查询商品分类
     * @param pid
     * @return
     */
    @ApiOperation(value = "根据父节点ID查询商品分类" , notes = "")
    @GetMapping("queryCategoryListByPid")
    public ResponseEntity<List<Category>> queryCategoryByPid(@ApiParam("父级ID")@RequestParam("pid") Long pid){
        return ResponseEntity.ok(categoryService.queryCategoryByPid(pid));
    }

    @ApiOperation(value = "新增类别" , notes = "")
    @PostMapping("insertCategory")
    public ResponseEntity<Void> insertCategory(@ApiParam("分类实体对象") Category category){
        categoryService.insertCategory(category);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @ApiOperation(value = "根据ID查询商品分类")
    @GetMapping("queryCategoryById")
    public ResponseEntity<Category> queryCategoryById(@ApiParam("ID") @RequestParam("id") Long id){
        Category category = categoryService.queryCategoryById(id);
        return ResponseEntity.ok(category);
    }

    @ApiOperation(value = "更新分类")
    @PutMapping("updateCategory")
    public ResponseEntity<Void> updateCategory(@ApiParam("更新分类实体对象") Category category ){
        categoryService.updateCategory(category);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

}
